<template>
    <div class="tags">
        <!-- tag盒子 -->
        <div class="tags__box tags__box--close">
        <el-tabs v-model="active" type="card" closable @tab-click="openTag" @edit="menuTag">
            <el-tab-pane
                    v-for="item in tagList"
                    :key="item.value"
                    :label="item.label"
                    :name="item.value">
            </el-tab-pane>
        </el-tabs>
            <el-dropdown class="tags__menu">
                <el-button type="primary"
                           size="mini">
                    更多
                    <i class="el-icon-arrow-down el-icon--right"></i>
                </el-button>
                <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="closeOthersTags">关闭其他</el-dropdown-item>
                    <el-dropdown-item @click.native="closeAllTags">关闭全部</el-dropdown-item>
                </el-dropdown-menu>
            </el-dropdown>
        </div>
    </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      active: ""
    };
  },
  computed: {
    ...mapGetters(["tagList", "tag"])
  },
  mounted() {
    this.setActive();
  },
  methods: {
    //激活当前选项
    setActive() {
      this.active = this.tag.value;
    },
    menuTag(value, action) {
      if (action === "remove") {
        let { tag, key } = this.findTag(value);
        this.$store.commit("DEL_TAG", tag);
        if (tag.value === this.tag.value) {
          tag = this.tagList[key === 0 ? key : key - 1]; //如果关闭本标签让前推一个
          this.openTag(tag);
        }
      }
    },
    openTag(item) {
      let tag;
      if (item.name) {
        tag = this.findTag(item.name).tag;
      } else {
        tag = item;
      }
      this.$router.push(tag.value);
    },
    findTag(value) {
      let tag, key;
      this.tagList.map((item, index) => {
        if (item.value === value) {
          tag = item;
          key = index;
        }
      });
      return { tag: tag, key: key };
    },
    closeOthersTags() {
      this.$store.commit("DEL_TAG_OTHER");
    },
    closeAllTags() {
      this.$store.commit("DEL_ALL_TAG");
      this.$router.push("/home");
    }
  },
  watch: {
    tag() {
      this.setActive();
    }
  }
};
</script>

<style lang="scss">
.tags {
  position: relative;
  padding: 5px 10px;
  margin-bottom: 10px;
  box-sizing: border-box;
  overflow: hidden;
  border-top: 1px solid #f6f6f6;
  background-color: #fff;
  box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  .el-tabs--card > .el-tabs__header {
    margin: 0;
  }
  .el-tabs--card > .el-tabs__header .el-tabs__nav,
  .el-tabs--card > .el-tabs__header .el-tabs__item,
  .el-tabs--card > .el-tabs__header {
    border: none;
  }
  .el-tabs--card > .el-tabs__header .el-tabs__item:first-child {
    border-left-width: 1px;
  }
  .el-tabs--card > .el-tabs__header .el-tabs__item {
    margin: 0 3px;
    height: 30px;
    line-height: 30px;
    font-size: 13px;
    font-weight: normal;
    border: 1px solid #eee;
    color: #ccc;
    border-radius: 3px;
    &.is-active {
      border: 1px solid #409eff;
      color: #409eff;
    }
  }
  .el-tabs__nav-prev,
  .el-tabs__nav-next {
    width: 20px;
    line-height: 34px;
    font-size: 18x;
    text-align: center;
  }
  &__box {
    position: relative;
    box-sizing: border-box;
    padding-right: 106px;
    width: 100%;
    &--close {
      .el-tabs__item {
        &:first-child {
          padding: 0 20px !important;
          .el-icon-close {
            display: none;
          }
        }
      }
    }
  }
  &__menu {
    position: absolute !important;
    top: 0;
    right: 0;
    padding: 1px 15px;
    box-sizing: border-box;
  }
}
</style>
